# Replication file 03c_Variable_Importance
# Article: Counterfactual Coercion: Could harsher sanctions against Russia have prevented the worst?
# Authors: Thies Niemeier, Gerald Schneider

# Set appropriate working directory
# setwd()

library(readxl)
library(tidyverse)

# Variable Importance
# EU
ImpositionEU <-  read_excel("Supplemental_Material/Variable_Importance/Variable_Importance_Imposition_EU_RF.xlsx")
names(ImpositionEU) <- c("Variable", "Importance")

# Rename variables
ImpositionEU <- ImpositionEU %>% 
  mutate(Variable = c("Dem. Index", "Pol. Terror 1", "Pol. Terror 2", "Pol. Terror 3", "Pol. Terror 4", "Pol. Terror 5",
                      "Successful Mil. Coup", "One-sided Violence", "Conflict",
                      "Violation Terr. Integrity", "GDPpc 2010 USD", "Sender Trade", "Target Oil/Gas Exports",
                      "Former Sender Colony", "Defense Alliance"))


# Create graph
ImpositionEU %>%
  mutate(Variable = factor(Variable, levels = Variable[order(Importance, decreasing = FALSE)]),
         Importance = as.numeric(Importance),
         Interests = as.factor(if_else(Variable %in% c("Dem. Index", "Pol. Terror 1", "Pol. Terror 2", "Pol. Terror 3", "Pol. Terror 4", "Pol. Terror 5",
                                                       "No Mil. Coup", "Unsuccessful Mil. Coup", "Successful Mil. Coup", "One-sided Violence", "Conflict",
                                                       "Violation Terr. Integrity"), "Lib. Norms", "Econ. & Political"))) %>%
  ggplot(mapping = aes(x=Variable, y = Importance, fill = Interests))+
  geom_bar(stat="identity") +
  scale_y_continuous(breaks = c(.2,.4,.6,.8,1))+
  scale_x_discrete()+
  coord_flip()+
  theme_minimal()

ggsave("Supplemental_Material/Variable_Importance/Plot_Variable_Importance_Imposition_EU_RF", device = "png", bg="white")

# US
ImpositionUS <- read_excel("Supplemental_Material/Variable_Importance/Variable_Importance_Imposition_US_RF.xlsx")
names(ImpositionUS) <- c("Variable", "Importance")

# Rename variables
ImpositionUS <- ImpositionUS %>% 
  mutate(Variable = c("Dem. Index", "Pol. Terror 1", "Pol. Terror 2", "Pol. Terror 3", "Pol. Terror 4", "Pol. Terror 5",
                      "Successful Mil. Coup", "One-sided Violence", "Conflict",
                      "Violation Terr. Integrity", "GDPpc 2010 USD", "Sender Trade", "Target Oil/Gas Exports",
                      "Former Sender Colony", "Defense Alliance"))


# Create graph
ImpositionUS %>%
  mutate(Variable = factor(Variable, levels = Variable[order(Importance, decreasing = FALSE)]),
         Importance = as.numeric(Importance),
         Interests = as.factor(if_else(Variable %in% c("Dem. Index", "Pol. Terror 1", "Pol. Terror 2", "Pol. Terror 3", "Pol. Terror 4", "Pol. Terror 5",
                                                       "No Mil. Coup", "Unsuccessful Mil. Coup", "Successful Mil. Coup", "One-sided Violence", "Conflict",
                                                       "Violation Terr. Integrity"), "Lib. Norms", "Econ. & Political"))) %>%
  ggplot(mapping = aes(x=Variable, y = Importance, fill = Interests))+
  geom_bar(stat="identity") +
  scale_y_continuous(breaks = c(.2,.4,.6,.8,1))+
  scale_x_discrete()+
  coord_flip()+
  theme_minimal()

ggsave(filename = "Supplemental_Material/Variable_Importance/Plot_Variable_Importance_Imposition_US_RF", device = "png", bg="white")
